package chatMainServer;

import hz.gyl.ChatMsgProto.ChatMsg;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.mina.core.session.IoSession;

import chat.util.proto.MessageUtil;
import chat.util.proto.PBMessage;


public class MessageHandler {
	private static MessageHandler messageHandler=new MessageHandler();
	private Map<Integer,IoSession> allplayere=new HashMap<Integer,IoSession>();
	private MessageHandler(){};	
	public static MessageHandler getIns(){
		return messageHandler;  
	}
	
	public void putPlayer(Integer playerId,IoSession iosession){
		this.allplayere.put(playerId, iosession);
	}
	
	
	public void sendAllPalyer(String msg,int playerId){
		ChatMsg.Builder response=ChatMsg.newBuilder();
		response.setMessage(msg);
		response.setMsgType(1);
		for(Entry<Integer,IoSession> entry:this.allplayere.entrySet()){	
			//System.out.println("------------1111111111111111111111111----------------"+entry.getKey());
			PBMessage req = MessageUtil.buildMessage(1000, playerId, response.build());
			entry.getValue().write(req);
		}
	}
	

}
